//index.js
//获取应用实例
import config from '../../common/config'
import http from '../../common/httpUtils.js'
import {
    Base64
} from '../../common/base64'
const app = getApp()

Page({
    data: {
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        timeReady: false,
        unionid: '',
        loginReady: false,
        loginInfo: null
    },
    onShow: function () {
        setTimeout(() => {
            this.timeReady = true;
            this.gotoPakjPage();
        }, 500)
    },
    onLoad: function () { // 处理获取微信用户信息
        this.login();
    },

    // 数据请求
    login: function () {
        wx.login({
            success: (res) => {
                var code = res.code;
                console.log(code);
                http.sendLogin(code).then(res => {
                    console.log(res)
                    if (res === null) {
                        return;
                        // 关闭小程序
                    } else {
                        if (res.code === "000000") {
                            this.loginReady = true;
                            this.islogin = res.data.isLogin;
                            this.openid = res.data.openId;
                            this.tk = res.data.token;
                            this.loginstatus = res.data.loginStatus;
                            this.session_key = res.data.session_key;
                            this.unionid = res.data.unionId;
                            this.gotoPakjPage();
                        } else if (res.code === "406406") {
                            wx.redirectTo({
                                url: '/pages/login/login?nouid=true'
                            })
                        } else {
                            this.openid = res.data.openId;
                            this.unionid = res.data.unionId;
                            let url = '/pages/login/login?';
                            if (this.openid) {
                                url += 'openid=' + this.openid;
                            }
                            if (this.unionid) {
                                url += '&unionid=' + this.unionid;
                            }
                            wx.redirectTo({
                                url: url
                            })
                        }
                    }
                }).catch(reason => {
                    console.log(reason);
                })
            }
        })
    },
    gotoPakjPage: function (openid) {
        if (this.timeReady && this.loginReady) {
            console.log("正在进入主界面");
            let url = '/pages/pakjh5/pakjh5?route=Login';
            if (this.openid) {
                url += '&oid=' + this.openid;
            }
            if (this.unionid) {
                url += '&uid=' + this.unionid;
            }
            if (this.tk) {
                url += '&tk=' + this.tk;
            }
            wx.reLaunch({
                url: url
            });
        }
    }
})